﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLibrary.BLL;
using System.Data.SqlClient;


namespace BusinessLibrary.DAL
{
    class sqlModule
    {
        public static Module ReaderToObject(SqlDataReader dr)
        {
            Module _module = new Module();
            _module.Id = int.Parse(dr["Id"].ToString());
            _module.Nom = dr["Nom"].ToString();


            if (!object.ReferenceEquals(dr["Id_classe"], DBNull.Value))
            {
                _module.classe = Classe.GetClasseById(int.Parse(dr["Id_classe"].ToString()));
            }
            return _module;
        }

        public static Module GetModuleById(int id)
        {
            Module module = new Module();
            string strSQL = @"select * from Module where UserId=@Id";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);
                    cmd.Parameters.AddWithValue("@Id", id);
                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        module = ReaderToObject(dr);
                    }
                    dr.Close();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return module;
        }

        public static int insertModule(Module m)
        {
            int n = 0;
            string strSQL = @"insert into Module (Nom,Id_classe) values(@nom, @Id_classe)";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);

                    cmd.Parameters.AddWithValue("@nom", m.Nom);
                    cmd.Parameters.AddWithValue("@Id_classe", m.classe.Id);

                    n = cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int updateModule(Module m)
        {
            int n = 0;

            string strSQl = @"UPDATE Module SET (@nom IS NULL OR Nom=@nom) Id_classe=@Id_classe where Id=@id ";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();

                    SqlCommand cmd = new SqlCommand(strSQl, cnx);

                    cmd.Parameters.AddWithValue("@nom", m.Nom);
                    cmd.Parameters.AddWithValue("@Id_classe", m.classe.Id);
                    n = cmd.ExecuteNonQuery();

                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="idAuthentif"></param>
        /// <returns></returns>
        public static int deletModule(int id, string nom, int idspecialite)
        {
            int n = 0;
            string strSQL = @"delete from Module where id=@id)";

            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);

                    cmd.Parameters.AddWithValue("@id", id);


                    n = cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }

        public static List<Module> SelectListeModule()
        {
            string strSQL = @"select * from Module ";
            using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
            {
                cnx.Open();

                SqlCommand cmd = new SqlCommand(strSQL, cnx);


                SqlDataReader dr = cmd.ExecuteReader();

                List<Module> List_module = new List<Module>();
                while (dr.Read())
                {
                    List_module.Add(ReaderToObject(dr));
                }

                dr.Close();
                cnx.Close();

                return List_module;
            }
        }



    }
}